package GUI;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.KeyboardFocusManager;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class VKBDigitsOnlyPopUp extends JDialog implements ActionListener {
	
	
	private JTextField txt;
	private Color btnColor;
	private Dimension screenSize;
	private int x;
	private int y;
	
	

	public VKBDigitsOnlyPopUp ()
	{
		super();
		setLayout(new GridLayout(4, 3,10,10));
		this.setFocusableWindowState(false);
        initialize();
    }
	
	public VKBDigitsOnlyPopUp (Color c )
	{
		super();
		this.btnColor=c;
		this.setFocusableWindowState(false);
		setLayout(new GridLayout(4, 3,10,10));
        initialize();
    }
	
	
	private void initialize()
	{
		screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		x=screenSize.width;
		y=screenSize.height;
		for(int i = 1; i <= 9; i++) 
        	createButton(Integer.toString(i));
        createEmptyButton();
        createNullButton();
        createBSpaceButton();
	}

	private void createEmptyButton()
    {
        JButton btn = new JButton();
        if(btnColor!=null)
        {
        	btn.setBackground(btnColor);
        }
        btn.setFocusable(false);
        btn.setFocusPainted(false);
        btn.setPreferredSize(new Dimension(100, 100));
        Font font = btn.getFont();
        float size = font.getSize() + 15.0f;
        btn.setFont(font.deriveFont(size));
        add(btn);
    }
	
	
    private void createButton(String label)
    {
        JButton btn = new JButton(label);
        if(btnColor!=null)
        {
        	btn.setBackground(btnColor);
        }
        
        btn.setFocusable(false);
        btn.addActionListener(this);
        btn.setFocusPainted(false);
        btn.setPreferredSize(new Dimension(100, 100));
        Font font = btn.getFont();
        float size = font.getSize() + 15.0f;
        btn.setFont(font.deriveFont(size));
        add(btn);
    }
    
    private void createNullButton()
    {
        JButton nullBtn = new JButton("0");
        if(btnColor!=null)
        {
        	nullBtn.setBackground(btnColor);
        }
        nullBtn.setFocusable(false);
        nullBtn.addActionListener(this);
        nullBtn.setFocusPainted(false);
        nullBtn.setPreferredSize(new Dimension(100, 100));
        Font font = nullBtn.getFont();
        float size = font.getSize() + 15.0f;
        nullBtn.setFont(font.deriveFont(size));
        add(nullBtn);
    }
    
    private void createBSpaceButton	()
    {
    	JButton bSpaceBtn=new JButton("BSpace");
    	bSpaceBtn.setFocusable(false);
    	if(btnColor!=null)
        {
    		bSpaceBtn.setBackground(btnColor);
        }
    	bSpaceBtn.addActionListener(new java.awt.event.ActionListener() 
		   {
			public void actionPerformed(java.awt.event.ActionEvent e) 
			{
				Component component=KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
		        JTextField txtF=(JTextField)component;
		        int caret=txtF.getCaretPosition();
		        String text=txtF.getText();	
		        
		        txtF.setText(text.substring(0,caret-1) + text.substring(caret));
		        txtF.setCaretPosition(caret-1);
			}
		});
    	
    	bSpaceBtn.setPreferredSize(new Dimension(100, 100));
        Font font = bSpaceBtn.getFont();
        float size = font.getSize()+x/140;
        bSpaceBtn.setFont(font.deriveFont(size));
        add(bSpaceBtn);
    }
    

    @Override
    public void actionPerformed(ActionEvent e)
    {
        String actionCommand = e.getActionCommand();
        Component component=KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
        JTextField txtF=(JTextField)component;
        int caret=txtF.getCaretPosition();
        String text=txtF.getText();
        
                   
        txtF.setText(text.substring(0,caret) +actionCommand+ text.substring(caret));
        txtF.setCaretPosition(caret+1);
        //txt.setText(txt.getText() + actionCommand);
    }
	
    
    public JTextField getTxt() {
		return txt;
	}

	public void setTxt(JTextField txt) {
		this.txt = txt;
	}
	
	public void setBtnColor(Color c)
	{
		this.btnColor=c;
	}
	

}
